<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-folio" tabindex="-1"><a class="header-anchor" href="#laravel-folio"><span>Laravel Folio</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#installation">安装</a>
<ul>
<li><a href="#page-paths-uris">页面路径 / URI</a></li>
<li><a href="#subdomain-routing">子域名路由</a></li>
</ul>
</li>
<li><a href="#creating-routes">创建路由</a>
<ul>
<li><a href="#nested-routes">嵌套路由</a></li>
<li><a href="#index-routes">Index 路由</a></li>
</ul>
</li>
<li><a href="#route-parameters">路由参数</a></li>
<li><a href="#route-model-binding">路由模型绑定</a>
<ul>
<li><a href="#soft-deleted-models">软删除模型</a></li>
</ul>
</li>
<li><a href="#render-hooks">渲染钩子</a></li>
<li><a href="#named-routes">命名路由</a></li>
<li><a href="#middleware">中间件</a></li>
<li><a href="#route-caching">路由缓存</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p><a href="https://github.com/laravel/folio" target="_blank" rel="noopener noreferrer">Laravel Folio</a> 是一款功能强大的基于页面的路由，旨在简化 Laravel 应用中的路由。使用 Laravel Folio，生成路由就像在应用的 <code v-pre>resources/views/pages</code> 目录中创建 Blade 模板一样轻松。</p>
<p>比如，要在 <code v-pre>/greeting</code> URL 上创建一个可访问的页面，只需在应用的 <code v-pre>resources/views/pages</code> 目录下创建一个 <code v-pre>greeting.blade.php</code> 文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>div<span class="token operator">></span></span>
<span class="line">    Hello World</span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>要开始使用 Folio，请使用 Composer 包管理器将 Folio 安装到你的项目中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/folio</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在安装完 Folio 后，你可用执行 <code v-pre>folio:install</code> Artisan 命令，该命令将在应用中安装 Folio 的服务提供者。此服务提供者注册了目录，Folio 将在该目录中搜索路由/页面：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan folio:install</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="页面路径-uris" tabindex="-1"><a class="header-anchor" href="#页面路径-uris"><span>页面路径 / URIs</span></a></h3>
<p>默认情况下，Folio 从应用的 <code v-pre>resources/views/pages</code> 目录中提供页面，但你可以在 Folio 服务提供者的 <code v-pre>boot</code> 方法中自定义这些目录。</p>
<p>比如，有时在同一个 Laravel 应用中指定多个 Folio 路径可能很方便。你可能希望为应用的「admin」区域提供一个单独的 Folio 页面目录，同时让应用其余页面使用另一个目录。</p>
<p>你可以使用 <code v-pre>Folio::path</code> 和 <code v-pre>Folio::uri</code> 方法实现。<code v-pre>path</code> 方法注册了一个目录，Folio 将在路由传入的 HTTP 请求时扫描该目录中的页面，而<code v-pre>uri</code> 方法则为该页面目录指定「基本 URI」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Folio<span class="token punctuation">\</span>Folio</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Folio</span><span class="token operator">::</span><span class="token function">path</span><span class="token punctuation">(</span><span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'views/pages/guest'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">uri</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Folio</span><span class="token operator">::</span><span class="token function">path</span><span class="token punctuation">(</span><span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'views/pages/admin'</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">uri</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/admin'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'*'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'auth'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'verified'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">            <span class="token comment">// ...</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="子域名路由" tabindex="-1"><a class="header-anchor" href="#子域名路由"><span>子域名路由</span></a></h3>
<p>你也可以基于传入请求的子域名路由到页面。比如，你可能希望将请求从 <code v-pre>admin.example.com</code> 路由到与 Folio 页面其他部分不同的页面目录。你可以通过在调用 <code v-pre>Folio::path</code> 方法之后调用 <code v-pre>domain</code> 方法来实现这一点：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Folio<span class="token punctuation">\</span>Folio</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Folio</span><span class="token operator">::</span><span class="token function">domain</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'admin.example.com'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">path</span><span class="token punctuation">(</span><span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'views/pages/admin'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>domain</code> 方法也允许你捕获域名或子域名的部分作为参数。这些参数将被注入到页面模板中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Folio<span class="token punctuation">\</span>Folio</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Folio</span><span class="token operator">::</span><span class="token function">domain</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{account}.example.com'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">path</span><span class="token punctuation">(</span><span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'views/pages/admin'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="创建路由" tabindex="-1"><a class="header-anchor" href="#创建路由"><span>创建路由</span></a></h2>
<p>你可以通过在任何 Folio 安装的目录中放置 Blade 模板来创建 Folio 路由。默认情况下，Folio 将挂载 <code v-pre>resources/views/pages</code> 目录，但你可以在 Folio 服务提供者的 <code v-pre>boot</code> 方法中自定义这些目录。</p>
<p>将 Blade 模板放入 Folio 安装的目录后，你可以立即通过浏览器访问它。比如，位于 <code v-pre>pages/schedule.blade.php</code> 中的页面可以在浏览器的 <code v-pre>http://example.com/schedule</code> 中访问。</p>
<p>要快速查看所有的 Folio 页面/路由列表，你可以调用 <code v-pre>folio:list</code> Artisan 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan folio:list</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="嵌套路由" tabindex="-1"><a class="header-anchor" href="#嵌套路由"><span>嵌套路由</span></a></h3>
<p>你可以在 Folio 的其中一个目录中通过创建一个或多个目录，创建嵌套路由。比如，要创建一个通过 <code v-pre>user/profile</code> 访问的页面，请在 <code v-pre>pages/user</code> 目录中创建一个 <code v-pre>profile.blade.php</code> 模板：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan folio:page user/profile</span>
<span class="line"></span>
<span class="line"><span class="token comment"># pages/user/profile.blade.php → /user/profile</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="index-路由" tabindex="-1"><a class="header-anchor" href="#index-路由"><span>Index 路由</span></a></h3>
<p>有时，你希望让某个给定页面成为目录中的「index」页。在 Folio 目录中放置一个 <code v-pre>index.blade.php</code> 模板，任何指向该路由的根目录将路由到此页面中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan folio:page index</span>
<span class="line"><span class="token comment"># pages/index.blade.php → /</span></span>
<span class="line"></span>
<span class="line">php artisan folio:page users/index</span>
<span class="line"><span class="token comment"># pages/users/index.blade.php → /users</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="路由参数" tabindex="-1"><a class="header-anchor" href="#路由参数"><span>路由参数</span></a></h2>
<p>通常，你需要将请求的 URL 片段插入到页面中，以便与它们进行交互。比如，你可能需要访问用户简介页面的用户 <code v-pre>ID</code>。为此，你可以将页面文件名的一段封装在方括号中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan folio:page <span class="token string">"users/[id]"</span></span>
<span class="line"></span>
<span class="line"><span class="token comment"># pages/users/[id].blade.php → /users/1</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>捕获的 URL 分段可以作为 Blade 模板中的变量进行访问：</p>
<div class="language-html line-numbers-mode" data-highlighter="prismjs" data-ext="html" data-title="html"><pre v-pre class="language-html"><code><span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">></span></span></span>
<span class="line">    User {{ $id }}</span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要捕获多个 URL 段，你可以使用三个号 <code v-pre>...</code> 作为 URL 分段的前缀；</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan folio:page <span class="token string">"users/[...ids]"</span></span>
<span class="line"></span>
<span class="line"><span class="token comment"># pages/users/[...ids].blade.php → /users/1/2/3</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当捕获多个 URL 分段时，捕获的分段将作为数组注入到页面中：</p>
<div class="language-html line-numbers-mode" data-highlighter="prismjs" data-ext="html" data-title="html"><pre v-pre class="language-html"><code><span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span><span class="token punctuation">></span></span></span>
<span class="line">    @foreach ($ids as $id)</span>
<span class="line">        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span>User {{ $id }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span></span>
<span class="line">    @endforeach</span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="路由模型绑定" tabindex="-1"><a class="header-anchor" href="#路由模型绑定"><span>路由模型绑定</span></a></h2>
<p>如果页面模板文件名的通配符分段对应于应用的 Eloquent 模型之一，Folio 将自动利用 Laravel 的路由模型绑定功能，并尝试将解析的模型实例注入页面：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan folio:page <span class="token string">"users/[User]"</span></span>
<span class="line"></span>
<span class="line"><span class="token comment"># pages/users/[User].blade.php → /users/1</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>捕获的模型可以作为 Blade 模板中的变量进行访问。模型的变量名称将转换为「驼峰式」：</p>
<div class="language-html line-numbers-mode" data-highlighter="prismjs" data-ext="html" data-title="html"><pre v-pre class="language-html"><code><span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">></span></span></span>
<span class="line">    User {{ $user->id }}</span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义键" tabindex="-1"><a class="header-anchor" href="#自定义键"><span>自定义键</span></a></h4>
<p>有时候你需要使用非 <code v-pre>id</code> 的字段解析绑定的 Eloquent 模型。要实现此功能，你可以指定页面文件名中的字段。比如，使用 <code v-pre>[Post:slug].blade.php</code> 文件名的页面将会尝试通过 <code v-pre>slug</code> 字段而非 <code v-pre>id</code> 字段来解析绑定的模型。</p>
<p>在 Windows 上，请使用 <code v-pre>-</code> 来分隔模型名和键名：<code v-pre>[Post-slug].blade.php</code>。</p>
<h4 id="model-位置" tabindex="-1"><a class="header-anchor" href="#model-位置"><span>Model 位置</span></a></h4>
<p>默认情况下，Folio 将在应用的 <code v-pre>app/Models</code> 目录中搜索模型。但是，如果需要，可以在模板的文件名中指定完全限定的模型类名：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan folio:page <span class="token string">"users/[.App.Models.User]"</span></span>
<span class="line"></span>
<span class="line"><span class="token comment"># pages/users/[.App.Models.User].blade.php → /users/1</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="软删除模型" tabindex="-1"><a class="header-anchor" href="#软删除模型"><span>软删除模型</span></a></h3>
<p>默认情况下，解析隐式模型绑定时不会检索已软删除的模型。但是，如果愿意，你可以通过调用页面模板中的 <code v-pre>withTrashed</code> 函数来指示 Folio 检索软删除的模型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">function</span> Laravel\Folio\<span class="token punctuation">{</span>withTrashed<span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">withTrashed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token delimiter important">?></span></span></span>
<span class="line"></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">></span></span></span>
<span class="line">    User {{ $user->id }}</span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="渲染钩子" tabindex="-1"><a class="header-anchor" href="#渲染钩子"><span>渲染钩子</span></a></h2>
<p>Folio 将返回页面 Blade 模板的内容，作为对传入请求的响应。但是，你可以通过调用页面模板中的 <code v-pre>render</code> 函数来自定义响应。</p>
<p><code v-pre>render</code> 函数接受一个闭包，该闭包将接收 Folio 正在渲染的 <code v-pre>View</code> 实例，允许你向视图添加其他数据或自定义整个响应。除了接收 <code v-pre>View</code> 实例之外，还将向 <code v-pre>render</code> 闭包提供所有额外的路由参数或模型绑定：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Post</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Auth</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>View<span class="token punctuation">\</span>View</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Folio<span class="token punctuation">\</span>render</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">render</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">View</span> <span class="token variable">$view</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Post</span> <span class="token variable">$post</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">can</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view'</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Unauthorized'</span><span class="token punctuation">,</span> <span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$view</span><span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photos'</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token operator">-></span><span class="token property">author</span><span class="token operator">-></span><span class="token property">photos</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span></span>
<span class="line"></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">></span></span></span>
<span class="line">    {{ $post->content }}</span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line"></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">></span></span></span>
<span class="line">    This author has also taken {{ count($photos) }} photos.</span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="命名路由" tabindex="-1"><a class="header-anchor" href="#命名路由"><span>命名路由</span></a></h2>
<p>你可以使用 <code v-pre>name</code> 函数指定给定页面的路由名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token keyword">function</span> <span class="token package">Laravel<span class="token punctuation">\</span>Folio<span class="token punctuation">\</span>name</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users.index'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>就如 Laravel 的命名路由，你可以使用 <code v-pre>route</code> 函数生成到已分配名称的 Folio 页面的 URL:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>a href<span class="token operator">=</span><span class="token string double-quoted-string">"{{ route('users.index') }}"</span><span class="token operator">></span></span>
<span class="line">    All Users</span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>a<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果页面中有参数，你可以将其传给 <code v-pre>route</code> 函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users.show'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="中间件" tabindex="-1"><a class="header-anchor" href="#中间件"><span>中间件</span></a></h2>
<p>通过调用页面模板中的 <code v-pre>middleware</code> 函数，你可以将中间件应用到指定页面：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">function</span> Laravel\Folio\<span class="token punctuation">{</span>middleware<span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'auth'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'verified'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token delimiter important">?></span></span></span>
<span class="line"></span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">></span></span></span>
<span class="line">    Dashboard</span>
<span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，要将中间件分配给一组页面，你可以在调用 <code v-pre>Folio::path</code> 方法后链式调用 <code v-pre>middleware</code> 方法。</p>
<p>要指定那些页面使用中间件，中间件数组的键请对想适用的页面使用对应的 URL 模式。字符 <code v-pre>*</code> 可以作为通配符使用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Folio<span class="token punctuation">\</span>Folio</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Folio</span><span class="token operator">::</span><span class="token function">path</span><span class="token punctuation">(</span><span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'views/pages'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'admin/*'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'auth'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'verified'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以在数组中间站中使用闭包来定义内联的匿名中间件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Closure</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Folio<span class="token punctuation">\</span>Folio</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Folio</span><span class="token operator">::</span><span class="token function">path</span><span class="token punctuation">(</span><span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'views/pages'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'admin/*'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'auth'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'verified'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Closure</span> <span class="token variable">$next</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$next</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="路由缓存" tabindex="-1"><a class="header-anchor" href="#路由缓存"><span>路由缓存</span></a></h2>
<p>使用 Folio 时，你应该始终使用 <a href="https://learnku.com/docs/laravel/11.x/routing#route-caching" target="_blank" rel="noopener noreferrer">Laravel 的路由缓存功能</a>。Folio 监听 <code v-pre>route:cache</code> 的 Artisan 命令，以确保 Folio 的页面定义及路由名正确地缓存以获得最大性能。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/foliomd/16719" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/fo...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/foliomd/16719" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/fo...</a></p>
</blockquote>
</div></template>


